<?php
namespace Home\Controller;
use Think\Controller;
class CoachController extends Controller{
  public function coachArrange(){
        $clubmodel=M('club');
        $coachmodel=M('coaches');

 $this->club=$clubmodel->order('clubid desc')->select();
          // var_dump($coachmodel);
            $this->coach=$coachmodel->join('club ON club.clubid=coaches.belongsclub')->select();
      //调用视图层显示结果
          $this->display();

  }
	

public function addCoach(){
		$coach = D('coaches');
		$coach->name = $_POST['cname'];
		$coach->coachname = $_POST['cname'];
		// $coach->coachpic = $_POST['coachpic'];
      if($_FILES["coachpic"]["error"]==0){
        	$info=$this->upload();
        	
        	$coach->coachpic=$info["coachpic"]["savename"];
      	}else{
        	$this->error("必须上传图片"); 
      	}
		$coach->instruction = $_POST['instruction'];
		$coach->address = $_POST['address'];
		$coach->phone = $_POST['phone'];
		$coach->belongsclub = $_POST['belongsclub'];
		// var_dump($coach);
		$result = $coach->add();
		if($result != flase){
			$this->success("添加大师成功！",U('Coach/coacharrange'));//url
		}else{
			$this->error("录入大师信息有错！");
		}
	}
  
	 public function updataCoach(){
        $id=$_POST['coachid'];
     
        $coach = M('coaches');
        $pic=$coach->where("coachid=".$id)->find();
        if($coach->create()){
            if ($_FILES['choachpic']['error'] == 0) {
                $info = $this->upload();
                // var_dump("id");
                $name=$info['coachpic']['savename'];
                unlink(realpath("./PUBLIC/photo/coachphoto/".$pic["coachpic"]));
            } else {
            	
                $name = $pic["coachpic"];
            }
            $coach->coachpic=$name;
            if (false !== $coach->where("coachid=".$id)->save()) {
                $this->success('修改成功！', U('Coach/coacharrange'));
            } else {         
                $this->error('数据错误','');           
            }
        }else {            
            $this->error($coach->getError());  
        }
    }
     public function upload(){
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->rootPath  =     './PUBLIC/photo/coachphoto/'; // 设置附件上传根目录
    $upload->autoSub=false;//关闭子目录，默认ture
    //$upload->savePath  =     ''; // 设置附件上传（子）目录,默认目录名为日期
    $info   =   $upload->upload();  // 上传文件 
    if(!$info) { 
    $this->error($upload->getError());    }
    else{
    return $info;   }                    
  }
  // public function delete(){
  //       $id=I('get.coachid');

  //       $goodsModel=M("coaches");
  //       $res=$goodsModel->delete($id);
  //        unlink(realpath("./PUBLIC/photo/".$adpicinfo["coachpic"]));
  //         var_dump("$id");
  //       if($res!=false){$this->success("删除成功");}
  //       else{$this->error("删除失败");}
  //   }
            public function delete(){

        $id=I('get.coachid');
        $coach=M("coaches");
        $pic = $coach->where("coachid=".$id)->field("coachpic")->find();
        $res=$coach->delete($id);
        unlink(realpath("./Public/photo/coachphoto/".$pic["coachpic"]));
        if($res!=false){$this->success("删除成功");}
        else{$this->error("删除失败");}
    }
}
?>